package com.lovo;

import jdk.jshell.execution.Util;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class FileUtil {
    /**
     * 序列化对象
     *
     * @param path 保存序列化对象的路径
     * @param obj  序列化对象
     */
    public static void writeObject(String path, Object obj) {
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(path);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 反序列化对象
     *
     * @param path 反序列化对象的路径
     * @return
     */
    public static Object readObject(String path) {
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            objectInputStream = new ObjectInputStream(fileInputStream);
            return (Object) objectInputStream.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return null;
    }

    /**
     * 删除文件夹
     *
     * @param Path
     */
    public static void delFile(String Path) {
        File file = new File(Path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                delFile(files[i].getAbsolutePath());
            }
            files[i].delete();
        }
        file.delete();
    }
//将下面字符串?后面的键值对保存到proerties文件中
// String url ="http://www.lovo.com/index?name=aaaaaa&age=20&gender=男";

    public static Properties ProPertices(String url) {
        Properties  properties = new Properties();
        String b = url.substring(url.indexOf("?") + 1);//name=aaaaaa&age=20&gender=男
        String[] c = b.split("&");//[name=aaaaaa, age=20, gender=男]
        for (int i = 0; i < c.length; i++) {
            String[] result = c[i].split("=");
            String resultKey = result[0];
            String resultValue = result[1];
            properties.setProperty(resultKey, resultValue);//输出
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("2.propertices");
            properties.store(fileOutputStream, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //输入
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("2.propertices");
            properties.load(fileInputStream);
//            System.out.println(properties.getProperty("name"));
//            System.out.println(properties.getProperty("age"));
//            System.out.println(properties.getProperty("gender"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return properties;
    }


    public static void main(String[] args) {
        System.out.println(  FileUtil.ProPertices("http://www.lovo.com/index?name=aaaaaa&age=20&gender=男").getProperty("name"));
      ;

    }
}
